package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import static io.reactivex.internal.schedulers.SchedulerPoolFactory.start;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.Tezhi3RecyAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.AgeBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.CustomClickListener;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils2;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class Tezhi3Activity extends BaseActivity {
    private String tokens;
    private RecyclerView rc_tezhi3;
    private Tezhi3RecyAdapter adapter;
    private TextView btn_goup;
    private int datingModeId, emotioRelationId, targetAgeId;
    private ImageView icon_back;
    public static Tezhi3Activity instance;

    @Override
    protected void initListener() {

        btn_goup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Tezhi3Activity.this, Tezhi4Activity.class);
                MmkvUtil.INSTANCE.encode("targetAgeId", targetAgeId);
                startActivity(intent);
            }
        });
        icon_back.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                finish();
            }

            @Override
            protected void onFastClick() {

            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            start();
            return false;

        } else {

            return super.onKeyDown(keyCode, event);

        }
    }


    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_tezhi3);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        instance = Tezhi3Activity.this;
        rc_tezhi3 = findViewById(R.id.rc_tezhi3);
        getRequest();
        btn_goup = findViewById(R.id.btn_goup);
        icon_back = findViewById(R.id.icon_back);
        tokens = MmkvUtil.INSTANCE.decodeString(TOKEN);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getAgeList(tokens, EXTRA);
    }

    private void getAgeList(String token, String extra) {
        NetUtils2.getInstance().getApiService().gettargetAgeList(token, extra)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStr", decryptStr);
                        Gson gson = new Gson();
                        AgeBean ageBean = gson.fromJson(decryptStr, AgeBean.class);

                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(Tezhi3Activity.this);
                        rc_tezhi3.setLayoutManager(linearLayoutManager);
                        adapter = new Tezhi3RecyAdapter(ageBean.getData().getTraitTargetAges());
                        rc_tezhi3.setAdapter(adapter);
                        adapter.setOnAddClickListener(new Tezhi3RecyAdapter.OnAddClickListener() {
                            @Override
                            public void onItemClick(int id) {
                                targetAgeId = id;
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
